﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Showroom.DAL;
using MyGeneration.dOOdads;

namespace Showroom.BLL
{
    public class Products : _Products
    {
        public static Products GetByID(int id)
        {
            try
            {
                Products prod = new Products();
                prod.LoadByPrimaryKey(id);
                return prod;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public static void DeleteAllProducts()
        {
            TransactionMgr tx = TransactionMgr.ThreadTransactionMgr();
            try
            {
                tx.BeginTransaction();
                //Delete all images
                ProductImages.DeleteAllImage();

                Products prods = new Products();
                prods.DeleteAll();
                prods.Save();
                tx.CommitTransaction();
            }
            catch (Exception ex)
            {
                tx.RollbackTransaction();
                throw (ex);
            }
        }

        public static void DeleteProductByID(int ID)
        {
            TransactionMgr tx = TransactionMgr.ThreadTransactionMgr();
            try
            {
                tx.BeginTransaction();
                //Delete all image of this product
                ProductImages.DeleteAllImageByProductID(ID);

                Products prod = new Products();
                prod.Where.ID.Value = ID;
                prod.Query.Load();
                prod.MarkAsDeleted();
                prod.Save();
                tx.CommitTransaction();
            }
            catch (Exception ex)
            {
                tx.RollbackTransaction();
                throw (ex);
            }
        }
    }
}
